home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / dos2unix.zip / DOS2UNIX.C next >
C/C++ Source or Header  |  1989-07-04  |  2KB  |  117 lines

  1. /*
  2.  *                              DOS2UNIX.C
  3.  *
  4.  * Clean out cr/lf combinations in a file but keep it's original
  5.  * date/time stamp.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #ifndef TRUE
  12. #    define TRUE  (1)
  13. #    define FALSE (0)
  14. #endif
  15.  
  16. #if MSDOS
  17. #    define link(x,y) rename (x, y)
  18. #    define R_CNTRL   "rb"
  19. #    define W_CNTRL   "wb"
  20. #else
  21. #    define R_CNTRL   "r"
  22. #    define W_CNTRL   "w"
  23. #endif
  24.  
  25.  
  26. struct stat s_buf;
  27.  
  28.  
  29.  
  30.  
  31. main (argc, argv)
  32. int argc;
  33. char **argv;
  34. {
  35.     char *path;
  36.     while (--argc>0)
  37.     {
  38.         if (stat (path=*++argv, &s_buf) != -1)
  39.         {
  40.             printf ("Dos2Unix: Cleaning file %s ...\n", path);
  41.             if (dos2u (path))
  42.             {
  43.                 fprintf (stderr, "Dos2Unix: Problems cleaning file %s\n", path);
  44.             }
  45.         }
  46.         else
  47.         {
  48.             fprintf (stderr, "Dos2Unix: Can't stat '%s'\n", path);
  49.         }
  50.     }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.     
  57.         
  58.     
  59. int
  60. dos2u (path)
  61. char *path;
  62. {
  63.     FILE *in, *out;
  64.     int ch,
  65.         rval = FALSE;
  66.     char temppath [16];
  67.     struct utimbuf { time_t actime, modtime; } ut_buf;
  68.  
  69.     strcpy (temppath, "./clntmp");
  70. #if !defined (MSDOS)
  71.     strcat (temppath, "XXXXXX");
  72.     mktemp (temppath);
  73. #endif    
  74.     if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
  75.         return TRUE;
  76.     if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
  77.     {
  78.         fclose (in);
  79.         return TRUE;
  80.     }
  81.     while ((ch = getc (in)) != EOF)
  82.         if ((ch != '\015' && ch != '\032') &&
  83.             (putc (ch, out) == EOF)           )
  84.         {
  85.             rval = TRUE;
  86.             break;
  87.         }
  88.     if (fclose (in) == EOF)
  89.     {
  90.         rval = TRUE;
  91.     }
  92.     if (fclose (out) == EOF)
  93.     {
  94.         rval = TRUE;
  95.     }
  96.     ut_buf.actime = s_buf.st_atime;
  97.     ut_buf.modtime = s_buf.st_mtime;
  98.     if (utime (temppath, &ut_buf) == -1)
  99.         rval = TRUE;
  100.     if (unlink (path) == -1)
  101.         rval = TRUE;
  102.     if (rval)
  103.     {
  104.         unlink (temppath);
  105.         return TRUE;
  106.     }
  107.     if (link (temppath,path) == -1)
  108.     {
  109.         fprintf (stderr, "Dos2Unix: Problems renaming '%s' to '%s'\n", temppath, path);
  110.         fprintf (stderr, "          However, file '%s' remains\n", temppath);
  111.         exit (1);
  112.     }
  113.     unlink (temppath);
  114.     return FALSE;
  115. }
  116.  
  117.